/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.VersionService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;
import com.sigmatrix.h5.web.vo.VersionVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *@ClassName: VersionController
 *@Description: app版本更新
 *@date 2017年8月8日 下午11:11:29
 */
@Api(tags = "版本更新")
@RestController
@RequestMapping("/app/version")
public class VersionController extends BaseController {
	
	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(VersionController.class);
	
	/**版本更新业务层接口*/
	@Autowired
	private transient VersionService versionService;
	
	@ApiOperation(value = "获取版本号")
	@PostMapping(value = "/getVersion")
	public Response getVersion(final HttpServletRequest request, @Valid @RequestBody final VersionVo versionVo,
			final BindingResult result) {
		LOGGER.info("获取版本号请求参数={}",JSONObject.toJSONString(versionVo));
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("获取版本号参数验证失败{}", errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}else{
				final Response response = versionService.getVersion(versionVo);
				LOGGER.info("获取版本号返回={}",JSONObject.toJSONString(response));
				return response;
			}
		} catch (Exception e) {
			LOGGER.error("获取版本号失败", e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
	
}
